<?php
    echo $this->Html->script('nav/switchNavigation');
?>
<h2>Edition des tricks </h2>
<div class="switchNavigation">
    <div class="navSwitch">
        
    </div>
    <?php
    $oldName = null;
    ?>
    
    <?php
    foreach ($tricksTypes as $tricksType){
        $name = $tricksType['TricksType']['name'];
        $tricksTypesSpins = $tricksType['TricksTypesSpin'];
        
        if ($name != $oldName && $oldName != null){
            ?>    
            </div>
        </div>
        <?php
        }
        
        
        if ($name != $oldName || $oldName == null){
            $oldName = $name;
            $multiple[$name] = array($tricksType['TricksType']['multiple']);
        ?>
        <div class="item" id="<?php echo $name;?>">
            <h4 class="title"> <?php echo $name;?> </h4>
            
            <div class="switchNavigation">
                <div class="navSwitch">
                    
                </div>
        <?php
        }
        else{      
            $multiple[$name][] = $tricksType['TricksType']['multiple'];
        }
    ?>
    
        
        <div class="item" id="<?php echo $name."_".$tricksType['TricksType']['multiple'];?>">
            <h5 class="title"> <?php echo $tricksType['TricksType']['multiple'];?> </h5>
            <h4> Rotations </h4>
            
            <table>
                <thead>
                    <tr>
                        <td>Tricks</td>
                    <?php
                    foreach ($sports as $sport){
                    ?>
                        <td><?php echo $sport['Sport']['name'];?></td>
                    <?php
                    }
                    ?>
                    </tr>
                </thead>
                <tbody>
            <?php
            foreach ($tricksTypesSpins as $tricksTypesSpin){
            ?>
                    <tr>
                        <td><?php echo $this->Display->trick($name, $tricksType['TricksType']['multiple'], $tricksTypesSpin['spin']);?></td>
                        <?php
                        foreach ($sports as $sport){
                            $sport = $sport['Sport'];
                        ?>
                            <td class="ajaxAction"> 
                                <?php 
                                echo $this->Form->input('Trick.tricks_types_spins', array(
                                    'type' => 'checkbox',
                                    'value' => $tricksTypesSpin['id'],
                                    'label' => '',
                                    'checked' => (!empty($tricksKeys[$sport['id'].'-'.$tricksTypesSpin['id']]) ? 'checked' : '')
                                ));?>
                                <?php 
                                echo $this->Form->input('Trick.sports_id', array(
                                    'type' => 'hidden',
                                    'value' => $sport['id'],
                                    'class' => 'sportId'
                                ));
                                
                                if (!empty($tricksKeys[$sport['id'].'-'.$tricksTypesSpin['id']])){
                                    $id = $tricksKeys[$sport['id'].'-'.$tricksTypesSpin['id']];
                                    echo $this->Form->input('Trick.id', array(
                                        'type' => 'hidden',
                                        'value' => $id,
                                        'class' => 'trickId'                                        
                                    ));
                                }
                                ?>
                                
                            </td>
                        <?php
                        }
                        ?>
                    </tr>
            <?php
            }
            ?>
                    
                </tbody>
            </table>
        </div>
    <?php   
    }
    ?>
    </div> 
</div>

<h2> Edition des figures </h2>

<?php

echo $this->Form->create('Trick');
echo $this->Form->input('name');
echo $this->Form->input('sports_id');
echo $this->Form->input('tricks_types_spins_id');
echo $this->Form->end('add');

?>

<script type="text/javascript">

$(document).ready(function(){
    
    settings = {
        debug : false
    };
    
    baseUrl = '<?php echo $this->webroot;?>';
    
    $('.ajaxAction input[type="checkbox"]').click(function(){
        if ($(this).attr('checked') == 'checked'){
            var action = 'add';
            var type = 'POST';
            var url = baseUrl + 'Tricks/' + action;
            var sportId = $('.sportId', $(this).parent().parent()).val();
            var tricksTypesSpinsId = $(this).val();
            
            var data = {
                sports_id : sportId,
                tricks_types_spins_id : tricksTypesSpinsId
            };
            
            if (settings.debug){
                alert(data.sports_id);
                alert(data.tricks_types_spins_id);
            }
        }
        else{
            var action = 'del';
            var type = 'GET';
            var id = $('.trickId', $(this).parent().parent()).val();
            
            if (settings.debug)
                alert(id);
            
            var url = baseUrl + 'Tricks/' + action + "/" + id;
            var data = {};
        }
        
        if (settings.debug){
            alert(url);
        }
        
        $.ajax({
            url : url,
            data : data,
            success: function(data){
                if (settings.debug){
                    alert(data);
                }
            },
            error: function(){
                alert('Erreur.');
            },
            type : type
        });
        
    });
    
});

</script>